package com.liyue.utillib;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import java.lang.reflect.Field;

/**
 * @author liyue
 * @emila 1078410438@qq.com
 * @data on 2021/9/11 13:21
 * Describe ：
 */
public class interfacejects {

    private static interfacejects instance;
    private String TAG = "s";
    private Class mC1;

    private interfacejects() {
    }

    public  static interfacejects getInstance() {
        if (instance == null) {
            instance = new interfacejects();
        }
        return instance;
    }

    public void injects(AppCompatActivity activity) {
        Class<? extends AppCompatActivity> clazz = activity.getClass();
        try {
            mC1 = Class.forName("com.liyue.utillib.MyBindView");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Field[] fields = clazz.getDeclaredFields();
        if (fields != null && fields.length > 0) {
            for (Field field : fields) {
                try {
                    field.setAccessible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    System.out.println(" "+field.getName() + "-----" +field.getAnnotation(mC1) +"-----"+field.getDeclaredAnnotation(mC1) );
                }
                //判断字段是否标注MyBindView
                MyBindView myBindView = field.getAnnotation(MyBindView.class);
                if (myBindView != null) {
                    //反射访问私有成员，必须加上这句
                    View view = activity.findViewById(myBindView.value());
                    if (view != null) {
                        try {
                            field.setAccessible(true);
                            field.set(activity, view);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }else{
                }
            }
        }
    }


    public static void initBindView(Object currentClass, View sourceView) {
        Field[] fields = currentClass.getClass().getDeclaredFields();
        if(fields != null && fields.length > 0) {
            Field[] var6 = fields;
            int var5 = fields.length;

            for(int var4 = 0; var4 < var5; ++var4) {
                Field field = var6[var4];
                MyBindView2 bindView = (MyBindView2)field.getAnnotation(MyBindView2.class);
                if(bindView != null) {
                    int viewId = bindView.id();
                    boolean clickLis = bindView.click();

                    try {
                        field.setAccessible(true);
                        if(clickLis) {
                            sourceView.findViewById(viewId).setOnClickListener((View.OnClickListener)currentClass);
                        }

                        field.set(currentClass, sourceView.findViewById(viewId));
                    } catch (Exception var11) {
                        var11.printStackTrace();
                    }
                }
            }
        }

    }

    public static void initBindView(Activity aty) {
        initBindView(aty, aty.getWindow().getDecorView());
    }

    public static void initBindView(View view) {
        Context cxt = view.getContext();
        if(cxt instanceof Activity) {
            initBindView((Activity)cxt);
        } else {
            Log.d("AnnotateUtil.java", "the view don\'t have root view");
        }
    }

    @TargetApi(11)
    public static void initBindView(Fragment frag) {
        initBindView(frag, frag.getActivity().getWindow().getDecorView());
    }
}
